package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.DefectSeverityException;
import com.management.commons.exception.ExceptionCode.DefectSeverityExceptionCode;
import com.management.controller.form.AddSeverityForm;
import com.management.controller.form.ChangeSeverityFrom;
import com.management.controller.form.GetALLSeverityFrom;
import com.management.controller.form.GetSeverityByNameForm;
import com.management.controller.info.GetAllSeverityInfo;
import com.management.controller.info.GetSeverityByNameInfo;
import com.management.model.DefectSeverity;
import com.management.dao.DefectSeverityDao;
import com.management.service.DefectSeverityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class DefectSeverityServiceImpl extends ServiceImpl<DefectSeverityDao, DefectSeverity> implements DefectSeverityService {

    @Override
    public Result findAllSeverity(GetALLSeverityFrom getALLSeverityFrom) {
        GetALLSeverityFrom page = page(getALLSeverityFrom);
        List<GetAllSeverityInfo> list = CopyUtils.CopyList(page.getRecords(), GetAllSeverityInfo.class);
        Page<GetAllSeverityInfo> page1=new Page<>();
        page1.setRecords(list);
        page1.setTotal(page.getTotal());
        page1.setSize(page.getSize());
        page1.setCurrent(page.getCurrent());
        page1.setPages(page.getPages());
        return Result.getInstance(Page.class).setData(page1);
    }

    @Override
    public Result addSeverity(AddSeverityForm addSeverityForm) {
        QueryWrapper<DefectSeverity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("severity_desc",addSeverityForm.getSeverityDesc());
        DefectSeverity one = getOne(queryWrapper);
        if(one!=null){
        throw new DefectSeverityException(DefectSeverityExceptionCode.SEVERITYDESC_EXISTS_ERROR);
        }
        if(addSeverityForm.getSeverityDesc()==null||addSeverityForm.getSeverityDesc().equals("")){
            return Result.getInstance().setCode(603).setMessage("请输入内容");
        }
        DefectSeverity defectSeverity = new DefectSeverity();
        defectSeverity.setCreateBy(addSeverityForm.getAccountName());
        defectSeverity.setModifyBy(addSeverityForm.getAccountName());
        defectSeverity.setSeverityDesc(addSeverityForm.getSeverityDesc());
        boolean b = saveOrUpdate(defectSeverity);
        if(b){
            return Result.getInstance().setCode(200).setMessage("添加成功");
        }
        throw new DefectSeverityException(DefectSeverityExceptionCode.ADDSEVERITY_ERROR);
    }

    @Override
    public Result changeSeverityState(Integer severityId) {
        DefectSeverity byId = getById(severityId);
        if(byId.getModuleState().equals("有效")){
            byId.setModuleState("禁用");
            saveOrUpdate(byId);
            return Result.getInstance().setCode(200).setMessage("禁用成功");
        }else{
            byId.setModuleState("有效");
            saveOrUpdate(byId);
            return Result.getInstance().setCode(200).setMessage("启用成功");
        }

    }

    @Override
    public Result checkSeverity(Integer severityId) {
        DefectSeverity byId = getById(severityId);
        if(byId.getModuleState().equals("有效")){
            return Result.getInstance().setCode(200);
        }
        throw new DefectSeverityException(DefectSeverityExceptionCode.SEVERITYSTATE_ERROR);
    }

    @Override
    public Result changeSeverity(ChangeSeverityFrom changeSeverityFrom) {
        QueryWrapper<DefectSeverity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("severity_desc",changeSeverityFrom.getSeverityDesc());
        DefectSeverity one = getOne(queryWrapper);
        if(one!=null){
            throw new DefectSeverityException(DefectSeverityExceptionCode.CHANGESEVERITY2_ERROR);
        }
        if(changeSeverityFrom.getSeverityDesc()==null||changeSeverityFrom.getSeverityDesc().equals("")){
            return Result.getInstance().setCode(602).setMessage("请输入内容");
        }
        DefectSeverity byId = getById(changeSeverityFrom.getSeverityId());

        byId.setSeverityDesc(changeSeverityFrom.getSeverityDesc());
        byId.setModifyBy(changeSeverityFrom.getAccountName());
        boolean b = saveOrUpdate(byId);
        if(b){
            return Result.getInstance().setCode(200).setMessage("修改成功");
        }
        throw new DefectSeverityException(DefectSeverityExceptionCode.CHANGESEVERITY_ERROR);
    }

    @Override
    public Result delSeverity(Integer severityId) {
        boolean b = removeById(severityId);
        if(b){
            return Result.getInstance().setCode(200).setMessage("删除成功");
        }
        throw new DefectSeverityException(DefectSeverityExceptionCode.DELSEVERITY_ERROR);
    }

    @Override
    public Result selByName(GetSeverityByNameForm getSeverityByNameForm) {
        QueryWrapper<DefectSeverity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("severity_desc",getSeverityByNameForm.getSeverityDesc());
        GetSeverityByNameForm page = page(getSeverityByNameForm, queryWrapper);
        List<GetSeverityByNameInfo> list = CopyUtils.CopyList(page.getRecords(), GetSeverityByNameInfo.class);
        Page<GetSeverityByNameInfo> infoPage=new Page<>();
        infoPage.setPages(page.getPages());
        infoPage.setCurrent(page.getCurrent());
        infoPage.setSize(page.getSize());
        infoPage.setTotal(page.getTotal());
        infoPage.setRecords(list);
        return Result.getInstance(Page.class).setData(infoPage);
    }

    @Override
    public Result getAllSeveritys() {
        List<DefectSeverity> list = list();
        return Result.getInstanceList(DefectSeverity.class).setData(list);
    }
}
